package com.ateam.biz.impl;

import java.util.Date;
import java.util.List;

import com.ateam.biz.UsersBiz;
import com.ateam.dao.CompaniesDao;
import com.ateam.dao.UsersDao;
import com.ateam.pojo.Users;
import com.ateam.util.DateFactory;

/** 
* @author WuYuan
* 
*/
public class UsersBizImpl implements UsersBiz {
	private UsersDao usersDao;
	private CompaniesDao companiesDao;
	
	public UsersDao getUsersDao() {
		return usersDao;
	}


	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}


	public CompaniesDao getCompaniesDao() {
		return companiesDao;
	}


	public void setCompaniesDao(CompaniesDao companiesDao) {
		this.companiesDao = companiesDao;
	}


	@Override
	public Users loginUser(Users u) {
		try {
			return usersDao.getUserLogin(u);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int registerUser(Users u) {
		try {
			u.setCompanyid(1);//设定初始公司id
			u.setHead("style/image/head/profileico2.jpg");//设定初始头像
			u.setJointime(DateFactory.getStrToDate(DateFactory.getDateToStr(new Date())));//设定注册时间
			usersDao.addUser(u);
			return companiesDao.addUserCompany(1, u.getUserid(), 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	
	@Override
	public List<Users> getUsersByCompanyid(int companyid) {
		try {
			return usersDao.getUsersByCompanyid(companyid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public List<Users> getUsersByProjectid(int projectid) {
		try {
			return usersDao.getUsersByProjectid(projectid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public List<Users> getUsersByNoProjectid(int companyid, int projectid) {
		try {
			return usersDao.getUsersByNoProjectid(companyid, projectid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public int addUserProject(int projectid, int[] memberids) {
		try {
			for(int i=0; i<=memberids.length-1;i++){
				usersDao.addUserProject(memberids[i], projectid);
			}
			return 1;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return 0;
	}


	@Override
	public List<Users> getUsersByGroupid(int groupid) {
		try {
			return usersDao.getUsersByGroupid(groupid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public List<Users> getUsersByNoGroupid(int companyid, int groupid) {
		try {
			return usersDao.getUsersByNoGroupid(companyid, groupid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public int addUserGroup(int groupid, int[] memberids) {
		try {
			for(int i=0; i<=memberids.length-1;i++){
				usersDao.addUserGroup(memberids[i], groupid);
			}
			return 1;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return 0;
	}


	@Override
	public List<Users> getCompanyApplyer(int companyid) {
		try {
			return usersDao.getCompanyApplyer(companyid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public int updateUserInfo(Users changeduser, Users currentuser) {
		currentuser.setUsername(changeduser.getUsername());
		currentuser.setDepartment(changeduser.getDepartment());
		currentuser.setDuty(changeduser.getDuty());
		currentuser.setAddress(changeduser.getAddress());
		currentuser.setQq(changeduser.getQq());
		currentuser.setAboutme(changeduser.getAboutme());
		currentuser.setTel(changeduser.getTel());
		currentuser.setHead(changeduser.getHead());
		try {
			return usersDao.updateUser(currentuser);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}



}
